package org.james.deleter.components;

import java.io.File;
import org.apache.commons.lang3.StringUtils;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

public class FileChooseGrid extends GridPane {
	
	private TextField textField1 = new TextField();
	private Button selectFileBtn = new Button("选择目录");
	private Label operateTypeLal = new Label("操作类型");
	private RadioButton operateTypeBtn1 = new RadioButton("仅删除文件");
	private RadioButton operateTypeBtn2 = new RadioButton("文件加文件夹");
	private RadioButton operateTypeBtn3 = new RadioButton("当前目录");
	private ToggleGroup operateTypeGroup = new ToggleGroup();
	private Label deleteTypeLal = new Label("删除类型");
	private RadioButton deteleTypeBtn1 = new RadioButton("按后缀名删除");
	private RadioButton deteleTypeBtn2 = new RadioButton("按匹配文字删除");
	private CheckBox isRecurDelete = new CheckBox("是否包括子目录");
	private ToggleGroup deteleTypeGroup = new ToggleGroup();
	private Label fileNameLal = new Label("文件(后缀)名");
	private TextField fileNameField = new TextField();
	private Button showExtensionsBtn = new Button("常用后缀名");
	private FileExtensionsPopup fileExtensionsPopup = null;
	private Stage stage = null;

	public FileChooseGrid(){
		super();
		this.initialize();
		this.bindingEvents();
	}
	
	public FileChooseGrid(Stage stage){
		super();
		this.initialize();
		this.bindingEvents();
		this.stage = stage;
	}

	private void initialize(){
		this.setAlignment(Pos.CENTER);
		this.setHgap(10);
		this.setVgap(20);
		this.setPadding(new Insets(5, 20, 10, 5));
		this.add(textField1, 1, 0, 3, 1);
		this.add(selectFileBtn, 5, 0, 1, 1);
		this.add(operateTypeLal, 0, 1, 1, 1);
		this.add(operateTypeBtn1, 1, 1, 1, 1);
		this.add(operateTypeBtn2, 2, 1, 1, 1);
		this.add(operateTypeBtn3, 3, 1, 1, 1);
		this.add(deleteTypeLal, 0, 2, 1, 1);
		this.add(deteleTypeBtn1, 1, 2, 1, 1);
		this.add(deteleTypeBtn2, 2, 2, 1, 1);
		this.add(isRecurDelete, 3, 2, 1, 1);
		this.add(fileNameLal, 0, 3, 1, 1);
		this.add(fileNameField, 1, 3, 3, 1);
		this.add(showExtensionsBtn, 5, 3, 1, 1);

		operateTypeBtn1.setToggleGroup(operateTypeGroup);
		operateTypeBtn2.setToggleGroup(operateTypeGroup);
		operateTypeBtn3.setToggleGroup(operateTypeGroup);
		operateTypeBtn1.setUserData("operateType1");
		operateTypeBtn2.setUserData("operateType2");
		operateTypeBtn3.setUserData("operateType3");
		operateTypeBtn1.setSelected(true);
		
		deteleTypeBtn1.setToggleGroup(deteleTypeGroup);
		deteleTypeBtn2.setToggleGroup(deteleTypeGroup);
		deteleTypeBtn1.setUserData("deteleType1");
		deteleTypeBtn2.setUserData("deteleType2");
		deteleTypeBtn1.setSelected(true);

		textField1.setMinWidth(300);
		fileNameField.setMaxWidth(250);
		
		isRecurDelete.setSelected(true);
	}
	
	private void bindingEvents(){
		showExtensionsBtn.setOnAction((event)->{
			if(fileExtensionsPopup != null){
				fileExtensionsPopup.show();
			}else{
				fileExtensionsPopup = new FileExtensionsPopup();
				fileExtensionsPopup.getConfirmBtn().setOnAction((e) -> {
					StringBuffer sb = new StringBuffer("");
					fileExtensionsPopup.getExtensionList().getItems()
						.stream().filter(item -> item.isSelected())
						.forEach((item) -> {
							if(sb.length() > 0){
								sb.append(" |"+item.getText());
							}else{
								sb.append(item.getText());
							}
							fileNameField.setText(sb.toString());
						}
					);
					fileExtensionsPopup.hide();
				});
				fileExtensionsPopup.show();
			}
		});
		
		deteleTypeGroup.selectedToggleProperty().addListener(
			(ObservableValue<? extends Toggle> ov, Toggle old_Toggle,Toggle new_Toggle) -> {
		        if (deteleTypeGroup.getSelectedToggle() != null && deteleTypeGroup.getSelectedToggle().getUserData() != null) {
		        	String userData = deteleTypeGroup.getSelectedToggle().getUserData().toString();
		        	if(StringUtils.equals("deteleType1", userData)){
		        		showExtensionsBtn.setDisable(false);
		        	}else if(StringUtils.equals("deteleType2", userData)){
		        		showExtensionsBtn.setDisable(true);
		        	}
		        }
			}
		);
		
		operateTypeGroup.selectedToggleProperty().addListener(
			(ObservableValue<? extends Toggle> ov, Toggle old_Toggle,Toggle new_Toggle) -> {
		        if (operateTypeGroup.getSelectedToggle() != null && operateTypeGroup.getSelectedToggle().getUserData() != null) {
		        	String userData = operateTypeGroup.getSelectedToggle().getUserData().toString();
		        	if(StringUtils.equals("operateType3", userData)){
		        		buttonToggles("operateType3",true);
		        	}
		        	if(StringUtils.equals("operateType2", userData)){
		        		buttonToggles("operateType3",false);
		        		buttonToggles("operateType2",true);
		        	}
		        	if(StringUtils.equals("operateType1", userData)){
		        		buttonToggles("operateType3",false);
		        		buttonToggles("operateType2",false);
		        		buttonToggles("operateType1",true);
		        	}
		        }
			}
		);
		
		selectFileBtn.setOnAction((evt) -> {
			DirectoryChooser directoryChooser = new DirectoryChooser();
			File forder = directoryChooser.showDialog(stage);
			this.textField1.setText(forder.getAbsolutePath());
		});
	}
	
	private void buttonToggles(String type,boolean b){
		if(StringUtils.isBlank(type)){
			return;
		}
		if(StringUtils.equals("operateType3", type)){
			deteleTypeBtn1.setDisable(b);
    		deteleTypeBtn2.setDisable(b);
    		fileNameField.setDisable(b);
    		showExtensionsBtn.setDisable(b);
		}
		if(StringUtils.equals("operateType2", type)){
			deteleTypeBtn1.setDisable(b);
    		showExtensionsBtn.setDisable(b);
    		deteleTypeBtn2.setSelected(true);
		}
		if(StringUtils.equals("operateType1", type)){
    		deteleTypeBtn1.setSelected(true);
		}
	}

	public TextField getTextField1() {
		return textField1;
	}

	public void setTextField1(TextField textField1) {
		this.textField1 = textField1;
	}

	public TextField getFileNameField() {
		return fileNameField;
	}

	public void setFileNameField(TextField fileNameField) {
		this.fileNameField = fileNameField;
	}

	public ToggleGroup getOperateTypeGroup() {
		return operateTypeGroup;
	}

	public void setOperateTypeGroup(ToggleGroup operateTypeGroup) {
		this.operateTypeGroup = operateTypeGroup;
	}

	public ToggleGroup getDeteleTypeGroup() {
		return deteleTypeGroup;
	}

	public void setDeteleTypeGroup(ToggleGroup deteleTypeGroup) {
		this.deteleTypeGroup = deteleTypeGroup;
	}

	public CheckBox getIsRecurDelete() {
		return isRecurDelete;
	}

	public void setIsRecurDelete(CheckBox isRecurDelete) {
		this.isRecurDelete = isRecurDelete;
	}
	
	
}
